package com.bm.order.process;

import com.bm.process.order.ArrayOfstring;
import com.bm.process.order.BaySize;
import com.bm.process.order.IOperationFacade;
import java.util.Arrays;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.bm.process.order.IOperationFacade.*;
import com.bm.process.order.ObjectFactory;
import com.bm.process.order.OperationFacade;
import com.bm.process.order.Order;

/**
 *
 * @author BV
 */
@WebService(serviceName = "TeamV_OrderProcessCentric")
@Stateless()
public class TeamV_OrderProcessCentric {
    
    private ObjectFactory f = new ObjectFactory();
      
    /** This web service will book the first available service bay according to date and size including a mechanic */
    @WebMethod(operationName = "CreateAndCalcQuote")
    @SuppressWarnings("CallToThreadDumpStack")
    public Order createAndCalcQuote( @WebParam(name = "name") String nameStr, @WebParam(name = "email") String emailStr, @WebParam(name = "parts") String partsStr, @WebParam(name = "size") String sizeStr ) {
    
        String name = nameStr;
        String email = emailStr;
        String parts = partsStr;
        String size = sizeStr;
        
        ArrayOfstring partsObj = new ArrayOfstring();
        List<String> partsList = partsObj.getString();
        
        //Quote for many parts
        if( parts.contains( ",")) {
            String[] partsArr = parts.split( "," );
            partsList.addAll( Arrays.asList( partsArr ));
        //quote for one part
        } else {
            partsList.add(partsStr);
        }
        
        
        //setup
        Order o = f.createOrder();
        o.setName( f.createOrderName(name));
        o.setEmail( f.createOrderEmail( email ) );
        o.setParts( f.createOrderParts( partsObj ));
        o.setId( new Integer(1));
        o.setStartDate( f.createOrderStartDate( "0000-00-00"));
        o.setEndDate( f.createOrderEndDate( "0000-00-00"));
        o.setStatus( f.createOrderStatus( "request" ));
        o.setTotalCost( new Integer(-1));
        o.setSize( f.createOrderSize( size ));
        
        //actually create qutoe
        Order quote = createQuote(o);
        
        return quote;
    }
    
    /** This method is called when quote is accepted by customer */
    @WebMethod(operationName = "AcceptQuote")
    @SuppressWarnings("CallToThreadDumpStack")
    public Order acceptQuote( @WebParam(name = "orderID") String orderIDStr ) {
        
        OperationFacade proxy = new OperationFacade();
        IOperationFacade service = proxy.getBasicHttpBindingIOperationFacade();
        
        Order o = service.getOrderByID( new Integer( orderIDStr ));
        Order order = service.updateOrderAndReservePartsBay( o );
            
        return order;
    }
    
    /** Reject when customer does not want to order */
    @WebMethod(operationName = "RejectQuote")
    @SuppressWarnings("CallToThreadDumpStack")
    public Order rejectQuote( @WebParam(name = "orderID") String orderIDStr ) {
        
        OperationFacade proxy = new OperationFacade();
        IOperationFacade service = proxy.getBasicHttpBindingIOperationFacade(); 
                
        Order o = service.rejectQuote( new Integer( orderIDStr ));
        
        return o;
        
    }
    
    /** Cancel order by id */
    @WebMethod(operationName = "CancelQuote")
    @SuppressWarnings("CallToThreadDumpStack")
    public Order cancelQuote( @WebParam(name = "orderID") String orderIDStr ) {
        
        OperationFacade proxy = new OperationFacade();
        IOperationFacade service = proxy.getBasicHttpBindingIOperationFacade(); 
                
        Order o = service.cancelOrderByID( new Integer( orderIDStr ));
        
        return o;
        
    }
    
    
    /*
     * This method sets up the quote
     */
    private Order createQuote( Order quote ) {
        
        OperationFacade proxy = new OperationFacade();
        IOperationFacade service = proxy.getBasicHttpBindingIOperationFacade();
        
        Order o = service.createAndCalcQuote(quote.getName().getValue(), quote.getEmail().getValue(), 
                                             quote.getParts().getValue(), BaySize.valueOf( quote.getSize().getValue() ));
        
        return o;
        
    }
    
    
}
